package com.newtouch.bxzs.business.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newtouch.bxzs.business.product.controller.ProductController;
import com.newtouch.bxzs.business.product.mapper.AdditionalMapper;
import com.newtouch.bxzs.business.product.model.AdditionalBO;
import com.newtouch.bxzs.business.product.model.AdditionalListBO;
import com.newtouch.bxzs.business.product.service.AdditionalService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.Additional;
import com.newtouch.bxzs.common.base.util.EntityToBeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 附加险实现类
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/13 13:45</td>
 * <td>lanwei</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author lanwei
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class AdditionalImpl implements AdditionalService {

    @Resource
    private AdditionalMapper additionalMapper;

    /**
     * 静态变量：系统日志
     */
    private static final Log logger = LogFactory.getLog(ProductController.class);

    /**
     * (non-Javadoc)
     * <p>
     * com.newtouch.bxzs.business.product.service.AdditionalService#getAdditionalList(java.lang.String)
     */
    @Override
    public AdditionalListBO getAdditionalList(String additionalInsuranceName, String mainInsuranceCode) {
        if (StringUtils.isEmpty(mainInsuranceCode)) {
            logger.info("查询列表失败");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        QueryWrapper<Additional> query = new QueryWrapper<>();
        /* 根据附加险名称查询 */
        if (!StringUtils.isEmpty(additionalInsuranceName)) {
            query.like("ADDITIONAL_INSURANCE_NAME", additionalInsuranceName);
        }
        query.eq("MAIN_INSURANCE_CODE", mainInsuranceCode);
        query.orderByAsc("ADDITIONAL_INSURANCE_NAME");
        /* 复制 */
        List<AdditionalBO> list = EntityToBeanUtils.copyBean(additionalMapper.selectList(query), AdditionalBO.class);
        /* 包装结构 */
        Map<String, List<AdditionalBO>> hashMap = new LinkedHashMap<>();
        for (AdditionalBO additionalBO : list) {
            /* map中存在此id，将数据存放当前key的map中 */
            if (hashMap.containsKey(additionalBO.getAdditionalInsuranceNameFullSpelling().substring(0, 1).toUpperCase())) {
                hashMap.get(additionalBO.getAdditionalInsuranceNameFullSpelling().substring(0, 1).toUpperCase())
                        .add(additionalBO);
            } else {// map中不存在，新建key，用来存放数据
                List<AdditionalBO> additionalBOList = new ArrayList<>();
                additionalBOList.add(additionalBO);
                hashMap.put(additionalBO.getAdditionalInsuranceNameFullSpelling().substring(0, 1).toUpperCase(),
                        additionalBOList);
            }
        }
        AdditionalListBO additionalListBO = new AdditionalListBO();
        /* 赋值 */
        additionalListBO.setAdditionalList(hashMap);
        additionalListBO.setTotal(list.size());
        return additionalListBO;
    }

}
